// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import GameControl_red_blue_man from "./GameControl_red_blue_man";
import Music, {musicName} from "../PublicScript/Music";
import GameData_red_blue_man, {specialType} from "./GameData_red_blue_man";

const {ccclass, property} = cc._decorator;

@ccclass
export default class PlaySpecialMusic extends cc.Component {

    camera = null;

    nodeWidthMax;
    nodeWidthMin;
    nodeHeightMax;
    nodeHeightMin;

    // type = null;

    onLoad () {
        //获取摄像机
        this.camera = cc.Camera.findCamera(cc.find("Canvas")).node;
    }

    start () {

    }

    musName = null;
    tag = 0;

    setValue(data){
        console.log("===========", data.name);
        this.musName = data.name;
        this.nodeWidthMax = data.widthMax;
        this.nodeWidthMin = data.widthMin;
        this.nodeHeightMax = data.HeightMax;
        this.nodeHeightMin =  data.HeightMin;
        this.tag = data.tag;
    }

    //播放音效
    playEffect(){
        if(GameControl_red_blue_man.gameState){
            return;
        }
        let widthMax = this.camera.x + cc.winSize.width/2;
        let widthMin = this.camera.x - cc.winSize.width/2;
        let heightMax = this.camera.y + cc.winSize.height/2;
        let heightMin = this.camera.y - cc.winSize.height/2;
        if(((this.nodeHeightMin > heightMin  && this.nodeHeightMin < heightMax) || (this.nodeHeightMax > heightMin  && this.nodeHeightMax < heightMax)) || (this.nodeHeightMin < heightMin && this.nodeHeightMax > heightMax)){
            if((this.nodeWidthMin > widthMin  && this.nodeWidthMin < widthMax)) {
                Music.specialMusic({name:this.musName, loop: true});
                this.judge(true);
            }
            if((this.nodeWidthMax > widthMin  && this.nodeWidthMax < widthMax)){
                Music.specialMusic({name: this.musName, loop: true});
                this.judge(true);
            }
        }
        if(this.nodeWidthMin > widthMax || this.nodeWidthMax < widthMin  || this.nodeHeightMin > heightMax  || this.nodeHeightMax < heightMin){
            this.judge(false);
        }
    }

    judge(state){
        switch (this.musName) {
            case musicName.water:
                GameData_red_blue_man.fluidStateList[this.tag] = state;
                for(let i of GameData_red_blue_man.fluidStateList){
                    if(i == true){
                        return;
                    }
                }
                Music.stopSpecialMusic({name: this.musName});
                break;
            case musicName.Mechanical_sound:
                GameData_red_blue_man.moveSideStateList[this.tag] = state;
                for(let i of GameData_red_blue_man.moveSideStateList){
                    if(i == true){
                        return;
                    }
                }
                Music.stopSpecialMusic({name: this.musName});
                break;
            case musicName.hair_drier_sound:
                GameData_red_blue_man.windStateList[this.tag] = state;
                for(let i of GameData_red_blue_man.windStateList){
                    if(i == true){
                        return;
                    }
                }
                Music.stopSpecialMusic({name: this.musName});
                break
        }
    }

    protected onDestroy(): void {
        Music.stopSpecialMusic({name: this.musName});
        switch (this.musName) {
            case musicName.water:
                GameData_red_blue_man.fluidStateList = [];
                break;
            case musicName.Mechanical_sound:
                GameData_red_blue_man.moveSideStateList = [];
                break;
            case musicName.hair_drier_sound:
                GameData_red_blue_man.windStateList = [];
                break
        }
    }

    protected onDisable(): void {
        this.judge(false);
    }

    update (dt) {
        this.playEffect();
    }
}
